﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Core.Utilities.Extensions;

namespace PhotoExhibition.Controllers
{
    public class HomeController : Controller
    {
        private string _folder;

        public ActionResult Index()
        {
            _folder = "Photos/photographybymilan.co.uk";
            var photosRootDirectory = Server.MapPath(_folder);

            var galleryFolders = Directory.GetDirectories(photosRootDirectory);

            var photoUrlsByGalleryFolder = new Dictionary<string, IEnumerable<string>>();

            galleryFolders.Each(galleryFolder =>
                                    {
                                        var galleryName = RemovePath(galleryFolder);
                                        var photoNames = Directory.GetFiles(galleryFolder, "*.jpg").Select(RemovePath);
                                        var photoUrls = photoNames.Select(photoName => string.Concat(_folder, "/", galleryName, "/", photoName));

                                        photoUrlsByGalleryFolder.Add(galleryName, photoUrls);
                                    });

            return View(photoUrlsByGalleryFolder);
        }

        private string RemovePath(string fileNameWithPath)
        {
            return fileNameWithPath.Substring(fileNameWithPath.LastIndexOf(@"\") + 1);
        }
    }

}
